﻿/*
 * 99% of this file is a copy of the GlobalServiceProvider system from mediaportals TvServer.
 * For the moment it is the only way I can get plugins working.
 */

/* 
 *	Copyright (C) 2005-2009 Team MediaPortal
 *	http://www.team-mediaportal.com
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */
using System;
using System.Collections.Generic;

namespace StreamTv2.PluginsProvider
{
  /// <summary>
  /// The service provider
  /// </summary>
  public class PluginsProvider
  {
    private readonly Dictionary<String, object> services;

    /// <summary>
    /// Constructor
    /// </summary>
    public PluginsProvider()
    {
        services = new Dictionary<String, object>();
    }

    /// <summary>
    /// This method returns true if the service is registered
    /// </summary>
    /// <param name="name">Name of the service</param>
    /// <returns>true if service is registered</returns>
    public bool IsRegistered(String name)
    {
        return services.ContainsKey(name);
    }

    /// <summary>
    /// Register a new service to the service provider
    /// </summary>
    /// <param name="name">Name of the service</param>
    /// <param name="service">service</param>
    public void Add(String name, object service)
    {
      // Make sure service implements type
      services.Add(name, service);
    }

    /// <summary>
    /// returns the service
    /// </summary>
    /// <param name="name">Name of the service</param>
    /// <returns>service itself</returns>
    /// <remarks>if service is not registered an exception is thrown</remarks>
    public object Get(String name)
    {
      if (services.ContainsKey(name))
      {
        return (object) services[name];
      }
      throw new ArgumentException(String.Format("Service {0} is not registered", name));
    }

    /// <summary>
    /// removes a service from the service provider
    /// </summary>
    /// <param name="name">Name of the service</param>
    public void Remove(String name)
    {
      if (services.ContainsKey(name))
      {
        services.Remove(name);
      }
    }

    /// <summary>
    /// Replaces an instance of the service 
    /// </summary>
    /// <param name="name">Name of the service to replace</param>
    /// <param name="service">new instance of the service</param>
    public void Replace(String name, object service)
    {
      services.Remove(name);
      services.Add(name, service);
    }
  }
}